package com.kaiyidiannao.activity.secondhandcar;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.kaiyidiannao.adapter.SecondHandCarAdapter;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandCar;
import com.kaiyidiannao.model.req.model.*;
import com.kaiyidiannao.view.AutoTextView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by guanzhe on 14-4-15.
 */
public class CarDisplayFragment extends Fragment implements View.OnClickListener, Observer {
    private LinearLayout linearLayout;//根view
    private AutoTextView mTextView02;
    private SecondHandCarAdapter secondHandCarAdapter = null;
//    private Button search;
    private ProgressBar progressBar;
//    ArrayList<SecondHandCar> topBeanList = DataCenter.secondHandCarTop;

    private TextView secondHandCarName,secondHandCarPrice,secondHandCarKM,secondHandCarAge;

    private List<SecondHandCar> secondHandCarLists = DataCenter.secondHandCar;
    private DragListView secondHandCarList;
    FragmentActivity secondHandCarActivity = null;

    private View no_content_view;//没有数据的布局对象

    //请求数据参数
    private int catid = DataCenter.secondHandCar_category_id;
    private int currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
    private boolean isClose = true;

    String brandName;
    int position;
    String title1,title2,title3;
    int page = DataCenter.LISTVIEW_DEFAULT_PAGE;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return linearLayout = (LinearLayout) inflater.inflate(R.layout.car_display, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        catid = DataCenter.secondHandCar_category_id;
        DataCenter.rolling_current_module_id = DataCenter.moduleFlag.secondhandcar;
       // DataCenter.rolling_current_cateid_id = catid;
//        getActivity().getClass().getName()



        secondHandCarActivity = getActivity();
        findView();

        secondHandCarList.setVisibility(View.INVISIBLE); // listview隐藏
        progressBar.setVisibility(View.VISIBLE);
        no_content_view.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onResume() {
        super.onResume();
        secondHandCarName = (TextView)secondHandCarActivity.findViewById(R.id.secondHandCarName);
        secondHandCarPrice = (TextView)secondHandCarActivity.findViewById(R.id.secondHandCarPrice);
        secondHandCarKM = (TextView)secondHandCarActivity.findViewById(R.id.secondHandCarKM);
        secondHandCarAge = (TextView)secondHandCarActivity.findViewById(R.id.secondHandCarAge);
//        search = (Button)secondHandCarActivity.findViewById(R.id.secondHandCarSearch);
        if (secondHandCarLists.size() <= 0) {
            initList();
        }

        if (getArguments() != null) {
            progressBar.setVisibility(View.GONE);
            secondHandCarList.setVisibility(View.VISIBLE);
            mTextView02.setVisibility(View.VISIBLE);
            if (getArguments().getString("flag").equals("flag1")) {
                brandName = getArguments().getString("brandName");
                DataCenter.brandName = brandName;
                secondHandCarName.setText(brandName);
                if (brandName.equals("全部品牌")){
                    DataCenter.brandName = "";
                }

                Log.d("flag1", getArguments().getInt("position") + "");
//                Log.d("brandName",brandName);
            }
            if (getArguments().getString("flag").equals("flag2")) {
                position = getArguments().getInt("position");
                title1 = getArguments().getString("list");
                secondHandCarPrice.setText(title1);
                Log.d("flag2", getArguments().getInt("position") + "");
            }
            if (getArguments().getString("flag").equals("flag3")) {
                position = getArguments().getInt("position");
                title2 = getArguments().getString("list");
                secondHandCarKM.setText(title2);
                Log.d("flag3", getArguments().getInt("position") + "");
            }
            if (getArguments().getString("flag").equals("flag4")) {
                position = getArguments().getInt("position");
                title3 = getArguments().getString("list");
                secondHandCarAge.setText(title3);
                Log.d("flag4", getArguments().getInt("position") + "");
            }
            progressBar.setVisibility(View.VISIBLE);
            if (brandName == null){
                DataCenter.brandName = "";
            }
            new GetSecondHandCarSearch(DataCenter.index1,DataCenter.brandName,DataCenter.index3,DataCenter.index2,page).execute();
//            search.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    progressBar.setVisibility(View.VISIBLE);
//                    if(brandName == null){
//                        new GetSecondHandCarSearch(DataCenter.index1,DataCenter.brandName,DataCenter.index3,DataCenter.index2,page).execute();
//                    }else{
//                        new GetSecondHandCarSearch(DataCenter.index1,DataCenter.brandName,DataCenter.index3,DataCenter.index2,page).execute();
//                    }
//                }
//            });
        } else {
            new GetSecondHandCarLIst(catid).execute();
        }
        if (!HttpUtils.isConnected()) {

        } else {
            new GetSecondHandCarLIstTop(catid).execute();
        }


        secondHandCarActivity = getActivity();
        findView();
    }
    //列表为空的时候执行
    private void initList() {
        if (secondHandCarLists == null || secondHandCarLists.size() <= 0) {
           // progressBar.setVisibility(View.VISIBLE);
        }
    }



    public void findView() {

        no_content_view = linearLayout.findViewById(R.id.no_content_view);

        secondHandCarList = (DragListView) linearLayout.findViewById(R.id.secondHandCarList);
        secondHandCarAdapter = new SecondHandCarAdapter(getActivity().getApplicationContext(), secondHandCarLists);
        secondHandCarList.setAdapter(secondHandCarAdapter);

        secondHandCarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(secondHandCarActivity, SecondHandCarDetail.class);
                Bundle bundle = new Bundle();
                intent.putExtra("catid",secondHandCarLists.get(position-1).getCatid());
                intent.putExtra("itemid", secondHandCarLists.get(position-1).getItemid());
                intent.putExtra("catid",secondHandCarLists.get(position-1).getCatid());
                intent.putExtra("addtime",secondHandCarLists.get(position-1).getAddtime());
                intent.putExtras(bundle);
                startActivity(intent);
                currentPage = position/10+1;
            }
        });
        secondHandCarList.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                if (getArguments() != null) {
                    new GetSecondHandCarSearch(DataCenter.index1,DataCenter.brandName,DataCenter.index3,DataCenter.index2,page).execute();
                    page = DataCenter.LISTVIEW_DEFAULT_PAGE;
                }else {
                    new GetSecondHandCarLIst(catid, DataCenter.LISTVIEW_DEFAULT_PAGE).execute();
                    currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
                }
            }

            @Override
            public void onLoadMore() {
                if (getArguments() != null) {
                    new GetSecondHandCarSearch(DataCenter.index1,brandName,DataCenter.index3,DataCenter.index2,++page).execute();
                }else {
                    new GetSecondHandCarLIst(catid, ++currentPage).execute();
                }
            }
        });

        mTextView02 = (AutoTextView) linearLayout.findViewById(R.id.switcher02);
        progressBar = (ProgressBar) linearLayout.findViewById(R.id.second_inner_progress_bar);
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;

        if(!HttpUtils.isConnected()){
            progressBar.setVisibility(View.GONE);
            no_content_view.setVisibility(View.VISIBLE);
            secondHandCarList.onRefreshComplete();
            secondHandCarList.onLoadMoreComplete(false);
        }

        if (flag == DataCenter.flag.GET_SECOND_HAND_CAR_LIST_SUCCESS
                || flag == DataCenter.flag.GET_SECOND_HAND_CAR_FAILED) { //类表数据
           //  secondHandCarLists.addAll(DataCenter.secondHandCar);

            secondHandCarList.setVisibility(View.VISIBLE); // listview
            progressBar.setVisibility(View.GONE);
            no_content_view.setVisibility(View.GONE);

            secondHandCarList.onRefreshComplete();
            secondHandCarList.onLoadMoreComplete(false);
            secondHandCarAdapter.notifyDataSetChanged();


            if (flag == DataCenter.flag.GET_SECOND_HAND_CAR_LIST_SUCCESS) {
                // Log.d(DataCenter.LOG_INFO, " DdcListActivity.update() -> SUCCESS");
                if ((secondHandCarLists == null || secondHandCarLists.size() < 1)) {
                    no_content_view.setVisibility(View.VISIBLE);
                    secondHandCarList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_empty));
                } else {
                    secondHandCarList.setVisibility(View.VISIBLE);
                    no_content_view.setVisibility(View.GONE);
                }
            }

            if (flag == DataCenter.flag.GET_SECOND_HAND_CAR_FAILED) {
                if ((secondHandCarLists == null || secondHandCarLists.size() < 1)) {
                    no_content_view.setVisibility(View.VISIBLE);
                    secondHandCarList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_error));
                } else {
                    no_content_view.setVisibility(View.GONE);
                }
            }
        } else { // 置顶滚动栏数据
           ArrayList<SecondHandCar> topBeanList = DataCenter.secondHandCarTop;
            mTextView02.setData3(topBeanList);
            mTextView02.startRolling();
        }

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, secondHandCarActivity + ".onDestroy");
        DataCenter.secondHandCarTop.clear();
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);


    }

}